(function(){
	var account_content = getEleByClassName("account-content")[0] ;
	var check_alls = getEleByClassName("check-all") ;
	var check_ones = getEleByClassName("check-one") ;
	sumTotal();
	account_content.onclick = function(e){
		var eve = e || window.event ;
		var target = e.target||e.srcElement ;
		console.log(target.className);
		switch (target.className){
			case "check-all" :
				for(var i = 0;i<check_ones.length;i++){
					check_ones[i].checked = target.checked ;
				}
				for(var i = 0;i<check_alls.length;i++){
					check_alls[i].checked = target.checked ;
				}
				break;
			case "check-one" :
				isCheckAll() ;
				sumTotal() ;
				break; 
			case "add" :				
				target.previousElementSibling.value++ ;
				var count = target.previousElementSibling.value ;
				if(count>1){
					target.previousElementSibling.previousElementSibling.innerHTML = " - " ;
				}
				toTotal(target,count);
				sumTotal()
				break;
			case "reduce":			
				if(target.nextElementSibling.value>1){
					target.nextElementSibling.value-- ;
				}
				var count = target.nextElementSibling.value ;
				if(count==1){
					target.innerHTML = " &nbsp; " ;
				}
				toTotal(target,count);
				sumTotal()
				break ;
			case "operate del":
				target.parentNode.remove();
				sumTotal();
				break ;
			case "delete-all":
				for(var i = check_ones.length-1 ;i>=0;i--){
					if(check_ones[i].checked){
						check_ones[i].parentNode.parentNode.remove();
					}
				}
				break;				
		}
	}





}())

function sumTotal(){
	var check_ones = getEleByClassName("check-one") ;
	var priceTotal = document.getElementById("priceTotal");
	var countTotal = document.getElementById("countTotal");
	var price = 0;
	var count = 0 ;
	for(var i = check_ones.length-1 ;i>=0;i--){
		if(check_ones[i].checked){
			var a = check_ones[i].parentNode.parentNode.getElementsByTagName("li")[6];
			price += parseInt(a.innerHTML);
			var b = check_ones[i].parentNode.parentNode.getElementsByTagName("li")[5].getElementsByTagName("input")[0].value ;
			count += parseInt(b);
		}
	}
	priceTotal.innerHTML = price ;
	countTotal.innerHTML = count ;

}




function toTotal(ele,num){
		var price = ele.parentNode.previousElementSibling.getElementsByTagName("span")[0].innerHTML ;
		var total = ele.parentNode.nextElementSibling ;
		total.innerHTML=price*num ;
}

function isCheckAll(){
	var check_alls = getEleByClassName("check-all") ;
	var check_ones = getEleByClassName("check-one") ;
	var isCheckd = true ;
	for(var i=0;i<check_ones.length;i++){
		if(check_ones[i].checked==false){
			 isCheckd = false ;
		}
	}
	for(var i = 0;i<check_alls.length;i++){
		check_alls[i].checked = isCheckd ;

	}
}



function getEleByClassName(className,element){
	var ele = element || document ;
	if(ele.getElementsByClassName){
		return ele.getElementsByClassName(className);
	}else{
		var all = document.getElementsByTagName("*");
		var arr = [] ;
		for(var i = 0;i<all.length;i++){
			var arrClassName = all[i].className.split(" ");
			for(var j = 0;j<arrClassName.length;j++){
				if(className==arrClassName[j]){
					arr.push(all[i]);
				}
			}
		}
		return arr ;
	}
}